package com.lianniu.web.tea.user.info.controller;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.user.favour.controller.TeaFavourController;
import com.lianniu.web.tea.user.info.model.TeaFrontusers;
import com.lianniu.web.tea.user.info.service.ITeaInfoService;
import com.lianniu.web.util.EmailUtils;
import com.lianniu.web.util.MD5Util;

@Controller
@RequestMapping("/rest/tea/info")
public class TeaInfoController {
	
	private final static Logger logger = Logger.getLogger(TeaInfoController.class);
	
	@Autowired
	private ITeaInfoService teaInfoService;
	/**
	 * 根据用户id查询用户信息
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/getTeaInfo",method = {RequestMethod.POST,RequestMethod.GET} )
	public @ResponseBody BaseResult getTeaInfo(HttpServletRequest req){
		logger.info("TeaInfoController getTeaInfo begin.");
		try {
			logger.info("getTeaInfo begin.");
			String userId = req.getParameter("userId");
			BaseResult baseResult = teaInfoService.getTeaInfo(userId);
			logger.info("getTeaInfo end.");
			logger.info("TeaInfoController getTeaInfo end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaInfoController getTeaInfo error:" + e.getMessage());
			logger.error("TeaInfoController getTeaInfo end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	
	/**
	 * fasong
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/updateWeixinForuser",method = {RequestMethod.POST,RequestMethod.GET} )
	public @ResponseBody BaseResult updateWeixinForuser(HttpServletRequest req, TeaFrontusers teaFrontusers){
		logger.info("TeaInfoController updateWeixinForuser begin.");
		try {
			logger.info("getTeaInfo begin.");
			String userId = req.getParameter("userId");
			String weixin = req.getParameter("weixin");
			String email = req.getParameter("email");
			if(StringUtils.isNotEmpty(email)){
				//校验邮箱可用
				EmailUtils.activateMail(teaFrontusers);
				teaFrontusers.setEmail(null);
			}
			BaseResult baseResult = teaInfoService.updateWeixinForuser(teaFrontusers);
			logger.info("updateWeixinForuser end.");
			logger.info("TeaInfoController updateWeixinForuser end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaInfoController updateWeixinForuser error:" + e.getMessage());
			logger.error("TeaInfoController updateWeixinForuser end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 修改密码
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/updatePasswordForuser",method = {RequestMethod.POST,RequestMethod.GET} )
	public @ResponseBody BaseResult updatePasswordForuser(HttpServletRequest req, TeaFrontusers teaFrontusers){
		logger.info("TeaInfoController updatePasswordForuser begin.");
		try {
			logger.info("updatePasswordForuser begin.");
			TeaFrontusers frontusers = teaInfoService.getTeaInfoByUserId(teaFrontusers.getUsersid().toString());
			if (frontusers != null && teaFrontusers.getUsersname().equals(frontusers.getUsersname())) {
				String loginpassword = teaFrontusers.getLoginpassword();
				String loginPassWord = MD5Util.getMD5String(loginpassword);
				teaFrontusers.setLoginpassword(loginPassWord);
				BaseResult baseResult = teaInfoService.updateWeixinForuser(teaFrontusers);
				return baseResult;
			}else{
				logger.info("updatePasswordForuser end.");
				logger.info("TeaInfoController updatePasswordForuser end.");
				return BaseResult.success("fail");
			}
		} catch (Exception e) {
			logger.error("TeaInfoController updatePasswordForuser error:" + e.getMessage());
			logger.error("TeaInfoController updatePasswordForuser end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * jieshou 
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/activatemail",method = {RequestMethod.POST,RequestMethod.GET} )
	public @ResponseBody BaseResult activatemail(HttpServletRequest request) throws IOException, AddressException, MessagingException, NoSuchAlgorithmException {
        //获取激活参数
        String email = request.getParameter("email");
        String token = request.getParameter("token");
        Long time = System.currentTimeMillis();
        TeaFrontusers u = teaInfoService.loadByToken(token);
        if(u == null){
        	return BaseResult.success("fail");
        }else{
        	u.setEmail(email);
        	teaInfoService.updateWeixinForuser(u);
        	return BaseResult.success("OK");
        }
    }
}
